package com.ftwj.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/api/session")
public class SessionTestController {

    @GetMapping("/set")
    public String setSession(HttpSession session) {
        session.setAttribute("testAttribute", "testValue");
        log.info("Session attribute set: testAttribute = testValue, Session ID: {}", session.getId());
        return "Session attribute set";
    }

    @GetMapping("/get")
    public String getSession(HttpSession session) {
        String testValue = (String) session.getAttribute("testAttribute");
        log.info("Retrieved session attribute: testAttribute = {}, Session ID: {}", testValue, session.getId());
        return "Session test attribute: " + testValue;
    }

    @GetMapping("/remove")
    public String removeSession(HttpSession session) {
        session.removeAttribute("testAttribute");
        log.info("Removed session attribute: testAttribute, Session ID: {}", session.getId());
        return "Session attribute removed";
    }

    @GetMapping("/clear")
    public String clearSession(HttpSession session) {
        session.invalidate();
        log.info("Session cleared, Session ID: {}", session.getId());
        return "Session cleared";
    }
}
